﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPMonitor.DataModel;
using NPMonitor.DataModel.Internal;
using NPMonitor.Implementation;
using NPMonitor.Implementation.Internal;
using NPMonitor.Interfaces;
using NPMonitor.Interfaces.Internal;
using NPMonitor.Helpers;
using System.Threading.Tasks;

namespace NPMonitor.Launcher.Tests.Service
{
    [TestClass]
    public class ServiceTest
    {
        [TestMethod]
        public void TimeSpanTest()
        {
            TimeSpan.FromSeconds(347875);
        }

        [TestMethod]
        public void KillProcessTest()
        {
            Utils.KillProcess("iexplore");
            // Utils.ExitProcess("iexplore");
        }

        [TestMethod]
        public void ServiceConfigTest()
        {
            ServiceConfig cfg, cfg2, cfg3;

            NPMonitor.Service.Instance.Start();

            IList<JobData> jobDatas = StoreManager.Current.JobStore.GetScheduleReadyJobs("Test-001", DateTime.Now);

            IList<int> jobIds = jobDatas.Where(j => j.JobTaskType == TaskType.Job).Select(j => j.TaskId.Value).ToList();

            IList<JobDefinition> jobDefinitions = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(
                jd => jobIds.Contains(jd.JobId.Value)).ToList();

            if (jobDefinitions.Count > 0)
            {
                jobDefinitions.All((jd) =>
                {
                    NPMonitor.Service.CheckAndUpdateJobDefinition(jd);
                    return true;
                });
            }

            cfg = NPMonitor.Service.Instance.Config;

            string cfgstr = Utils.SerializeObject(NPMonitor.Service.Instance.Config, NPMonitor.Service.Instance.Config.GetType());
            cfg3 = Utils.DeserializeObject(cfgstr, NPMonitor.Service.Instance.Config.GetType()) as ServiceConfig;

            NPMonitor.Service.Instance.Stop(false);

            NPMonitor.Service.Instance.Start();

            cfg2 = NPMonitor.Service.Instance.Config;

            NPMonitor.Service.Instance.Stop(false);

            Assert.AreEqual(cfg.JobDefinitions.Count, cfg2.JobDefinitions.Count, cfg3.JobDefinitions.Count);
        }

        [TestMethod]
        public void ServiceExecutionTest()
        {
            NPMonitor.Service.Instance.Start();

            Thread.Sleep(TimeSpan.FromSeconds(100));

            NPMonitor.Service.Instance.Stop(true);
        }

        [TestMethod]
        public void RegisterToServerTest()
        {
            MachineInfo mi = Utils.GetMachineInfo();
        }

        [TestMethod]
        public void ThreadTest()
        {
            Thread t = new Thread(() => {
                int m=0;
                while (true)
                {
                    Console.WriteLine(string.Format("Thread Times:{0}", ++m));
                    Thread.Sleep(2000);
                }
            });

            t.Start();
            Console.WriteLine("Thread Join 20 minutes");
            if (!t.Join(10 * 1000))
            {
                t.Abort();
            }

            Console.WriteLine("Thread Done.");

            //Task task = Task.Factory.StartNew(() => { });
            //task.Wait(


        }
    }
}
